#include "Main.h"

byte* cpetPtr;
byte* cpetmgrPtr;
byte* GetPetIdByIndexFunctionPtr;
byte* SummonPetFunctionPtr;

void CPet::Init()
{
	__try {
		cpetPtr = (byte*)(*(dword*)Mem->GetAddress("CPetPtr"));
		cpetmgrPtr = (byte*)(*(dword*)Mem->GetAddress("CPetMgrPtr"));
		GetPetIdByIndexFunctionPtr = Mem->GetAddress("GetPetIdByIndexFunction");
		SummonPetFunctionPtr = Mem->GetAddress("SummonPetFunction");
	}
	__except(1) { }
}

unsigned long CPet::GetCurrentHp()
{
	if(!cpetPtr)
		return NULL;

	__try {
		return *(dword*)(cpetPtr + 0xFD8);
	}
	__except(1) {
		return NULL;
	}
}

unsigned long CPet::GetMaxHp()
{
	if(!cpetPtr)
		return NULL;

	__try {
		return *(dword*)(cpetPtr + 0xFDC);
	}
	__except(1) {
		return NULL;
	}
}

unsigned long CPet::GetCurrentMp()
{
	if(!cpetPtr)
		return NULL;

	__try {
		return *(dword*)(cpetPtr + 0x1300);
	}
	__except(1) {
		return NULL;
	}
}

unsigned long CPet::GetMaxMp()
{
	if(!cpetPtr)
		return NULL;

	__try {
		return *(dword*)(cpetPtr + 0x1304);
	}
	__except(1) {
		return NULL;
	}
}

unsigned long CPet::GetPetIdByIndex(unsigned long index)
{
	unsigned long retVal;

	if(!cpetmgrPtr || !GetPetIdByIndexFunctionPtr)
		return NULL;

	_asm {
		PUSH index
		MOV ECX,cpetmgrPtr
		CALL GetPetIdByIndexFunctionPtr
		MOV retVal,EAX
	}
}

void CPet::SummonPet(unsigned long index)
{
	if(!cpetmgrPtr || !SummonPetFunctionPtr)
		return;

	dword petId = CPet::GetPetIdByIndex(index);
	dword extraParam = *(dword*)(cpetmgrPtr + 0x1C);

	_asm {
		PUSH extraParam
		PUSH petId
		MOV ECX,cpetmgrPtr
		CALL SummonPetFunctionPtr
	}
}

long CPet::GetActivePetPlayerId()
{
	__try {
		return *(long*)(cpetmgrPtr);
	}
	__except(1) {
		return NULL;
	}
}